package com.zbl.singleDemo;

import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Header;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author zbl1997
 * @date 2022/11/25
 * @desc
 */
public class Mp3Analysis {

    public static void main(String[] args) {
        getMp3Length();
    }

    public static void getMp3Length() {
        BufferedInputStream bis = null;
        Bitstream bitstream = null;
        try {
            URL url = new URL("http://crm-wxwork-cdn.highso.com.cn/20/meeting_voice_call/20221124/08b76e4197b04c008b9c6b6e31d73c00_8773999289884726225_1669256621571_external.mp3");
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            urlConn.connect();
            //此url响应头中包含"Accept-Length"为字节数大小
            String headerField = urlConn.getHeaderField("Content-Length");
            bis = new BufferedInputStream(urlConn.getInputStream());

            bitstream = new Bitstream(bis);
            Header header = bitstream.readFrame();
            int bitrate = header.bitrate();
            //根据文件大小计算 字节数*8/码率/1000（毫秒值转秒）
            int timeLong = Integer.parseInt(headerField)*8/bitrate;
            System.out.println(timeLong);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bitstream!=null){
                try {
                    bitstream.close();
                } catch (BitstreamException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
